package com.android.server.util;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Properties;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.commons.lang.StringUtils;


public class Configration extends Properties {
	private static final long serialVersionUID = 1L;
	private static final Configration conf = new Configration();
	private static final String CONF_FILE_NAME = "/conf/conf.properties";

	private Configration() {
		InputStream is = null;
		try {
			is = this.getClass().getResourceAsStream(CONF_FILE_NAME);
			load(is);
			if(is != null){
				is.close();
			}
		} catch (IOException e) {
			e.printStackTrace();
		} 
	}
	
	public static Configration getInstance(){
		return conf;
	}
	
	public double getDouble(String key){
		String value = getProperty(key);
		Pattern pattern = Pattern.compile("[0-9]+[.]?[0-9]*");
		Matcher matcher = pattern.matcher(value);
		if(matcher.matches()){
			return new Double(value);
		}
		throw new IllegalArgumentException("This key is't mapping to a double");
	}
	
	public int getInt(String key){
		String value = getProperty(key);
		if(StringUtils.isNumeric(value)){
			return new Integer(value);
		}
		return -1;
	}
	
	public String[] getArray(String key){
		String value = getProperty(key);
		if(value.startsWith("[") && value.startsWith("]")){
			int startIndex = value.indexOf('[') + 1;
			int endIndex = value.lastIndexOf(']');
			String insideContent = value.substring(startIndex, endIndex);
			return insideContent.split(",");
		}else{
			String[] tempArray = {value};
			return tempArray;
		}
	}
}
